package com.youchuang.action.cart;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ActionSupport;
import com.youchuang.action.BaseAction;
import com.youchuang.common.Constant;
import com.youchuang.service.CartService;

import net.sf.json.JSONArray;

/**
 * 购物车
 * 
 * @author Winnie
 *
 */
@Namespace(value = "/cart")
@ParentPackage("shopping-default")
public class CartAction extends BaseAction {
	private static final Logger logger = Logger.getLogger(CartAction.class.getSimpleName());
	private String id;
	private int num;
	private boolean flag;
	@Resource
	private CartService cartService;

	public String execute() throws Exception {
		logger.info("method is execute");
		return Constant.SUCCESS;
	}

	@Action(value = "cartBuy", interceptorRefs = { @InterceptorRef(value = "loginStack") }, results = {
			@Result(name = ActionSupport.SUCCESS, type = "json") })
	public String buy() throws Exception {
		logger.info("method is buy");
		cartService.buy(id);
		flag = true;
		return Constant.SUCCESS;
	}

	@Action(value = "viewAction", interceptorRefs = { @InterceptorRef(value = "loginStack") }, results = {
			@Result(name = ActionSupport.SUCCESS, location = "/WEB-INF/jsp/cart/cart_list.jsp") })
	public String view() throws Exception {
		logger.info("method is view");
		return Constant.SUCCESS;
	}

	@Action(value = "deleteAction", interceptorRefs = { @InterceptorRef(value = "loginStack") }, results = {
			@Result(name = ActionSupport.SUCCESS, location = "/WEB-INF/jsp/cart/cart_list.jsp") })
	public String delete() throws Exception {
		logger.info("method is delete");
		cartService.delete(id);
		return Constant.SUCCESS;
	}

	@Action(value = "recoveryAction", interceptorRefs = { @InterceptorRef(value = "loginStack") }, results = {
			@Result(name = ActionSupport.SUCCESS, location = "/WEB-INF/jsp/cart/cart_list.jsp") })
	public String recovery() throws Exception {
		logger.info("method is recovery");
		cartService.recovery(id);
		return Constant.SUCCESS;
	}

	@Action(value = "modifyAction", interceptorRefs = { @InterceptorRef(value = "loginStack") }, results = {
			@Result(name = ActionSupport.SUCCESS, location = "/WEB-INF/jsp/cart/cart_list.jsp") })
	public String modify() throws Exception {
		logger.info("method is modify");
		cartService.update(id, num);
		return Constant.SUCCESS;
	}

	@Action(value = "clearAction", interceptorRefs = { @InterceptorRef(value = "loginStack") }, results = {
			@Result(name = ActionSupport.SUCCESS, location = "/WEB-INF/jsp/cart/cart_list.jsp") })
	public String clear() throws Exception {
		logger.info("method is clear");
		cartService.clearCart();
		return Constant.SUCCESS;
	}

	public List<CartItem> getCiBuy() throws Exception {
		logger.info("method is getCiBuy");
		List<CartItem> list = cartService.getItems(false);
		logger.info("response json ciBuy = " + JSONArray.fromObject(list).toString());
		return list;
	}

	public List<CartItem> getCiDel() throws Exception {
		logger.info("method is getCiDel");
		return cartService.getItems(true);
	}

	public double getTotalFixed() throws Exception {
		logger.info("method is getTotalFixed");
		return cartService.total(true);
	}

	public double getTotalDang() throws Exception {
		logger.info("method is getTotalDang");
		return cartService.total(false);
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

}
